replaceRichText with richText

I'm trying to replace plain text "TM" with superscripted "TM" in object text and want to preserver the rest of the rich text formatting. I am able to create the rich text replacement string so that if I set the object text to newtxt, I see the superscripting. However, if I invoke replaceRichText passing in the original rich string object text and have it replace the ROMEOTM with newstring, it doesn't display the superscripting when I set object text to the resulting string. I'm getting an extra backslash in the object text. I had this working at one time, but lost the script to do it. And old age must be setting in because for the life of me, I can't get it to work again. Any ideas what I'm doing wrong? Here is the code...

Module m = current
Object o
string otxt
string newtxt = "ROMEO\\super TM\\nosupersub"
RichText rt
int strt
int len

for o in m do {
otxt = probeRichAttr_(o, "Object Text")
if (null otxt) continue
for rt in otxt do {
if (!matches("ROMEOTM", rt.text)) continue
while (matches("ROMEOTM", otxt)) {
findRichText(otxt, "ROMEOTM", strt, len, true)
otxt = replaceRichText(otxt, strt, len, newtxt)
}
}
o."Object Text" = richText (otxt)
}

Kind regards,
John
Jagtattoo - Tue Feb 05 17:33:56 EST 2013

Re: replaceRichText with richText
Jagtattoo - Tue Feb 05 23:02:40 EST 2013

After further investigation and various attempts for a workaround, I could not get replaceRichText to work as expected when the replacement string is rich text. However, if I use the findRichText command to locate the start and length target string, and concatenate the new string, the superscripting works. So, instead of

otxt = replaceRichText(otxt, strt, len, newtxt)

I used...

otxt = otxt0:strt-1 newtxt otxtstrt+len:length(otxt)-1

This worked. Does anyone have a better suggestion?

Re: replaceRichText with richText
llandale - Wed Feb 06 11:42:45 EST 2013

You don't need the "for rt in otext do" loop. At first glance it seems that "while (matches("ROMEOTM", otxt)) {" will match even after you substitute, but I suppose it won't since it is looking for the complete rich text string which changes.

In the old days I had to convert the rich text to raw text; then search the raw text and use the position and offset into cutRichText. I suppose I'll give "findRichText" and "replaceRichText" another try.

I believe this is a better replacement string which only matters if (unlikely) "ROMEO" was already superscripted.
  • "ROMEO{\\super TM}"

-Louie

Re: replaceRichText with richText
llandale - Wed Feb 06 11:48:37 EST 2013

Jagtattoo - Tue Feb 05 23:02:40 EST 2013
After further investigation and various attempts for a workaround, I could not get replaceRichText to work as expected when the replacement string is rich text. However, if I use the findRichText command to locate the start and length target string, and concatenate the new string, the superscripting works. So, instead of

otxt = replaceRichText(otxt, strt, len, newtxt)

I used...

otxt = otxt0:strt-1 newtxt otxtstrt+len:length(otxt)-1

This worked. Does anyone have a better suggestion?

Yes, replaceRichString expects to replace raw text. So rebuilding the string Before Replace After seems to be the way to go.

Seems though Buffers would work more effeciently

-Louie